﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ServiceGerenciador.Model;
using System.Collections.Generic;

public partial class paginas_Estoque : System.Web.UI.Page
{
    protected int ModelId
    {
        get
        {
            if (Request.QueryString["Id"] != null)
            {
                return Convert.ToInt32(Request.QueryString["Id"]);
            }
            else
            {
                return 0;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Load();   
    }

    protected void Load()
    {
        if (ModelId > 0)
        {
            Prod prod = Prod.Find(ModelId);
            txtNomeProduto.Text = prod.produto;
            litQnt.Text = prod.estoque.ToString();
        }
    }

    protected void btn_pedit_Click(object sender, EventArgs e)
    {
        int qnt;

        if (int.TryParse(txtQtdeEstoque.Text, out qnt))
        {
            Prod prod = Prod.Find(ModelId);

            if (rbListOperacao.SelectedIndex == 0)
            {
                prod.estoque = prod.estoque + qnt;
            }
            else
            {
                prod.estoque = prod.estoque  - qnt;
            }

            prod.Save();
            
            ApplicationEnvironment.Instance.Flash["Notice"] = "Estoque atualizado com sucesso!";

            Response.Redirect("Estoque.aspx");
        }

        Load();
    }
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("Estoque.aspx");
    }
}
